package com.example.myapplication;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;
import com.example.myapplication.fragment.AnimationFragment;
import com.example.myapplication.fragment.FragmentAdapter;
import com.example.myapplication.fragment.WordFragment;
//import com.example.myapplication.service.MusicController;
//import com.example.myapplication.service.MusicService;
import com.example.myapplication.service.MusicController;
import com.example.myapplication.service.MusicService;
import com.example.myapplication.utils.Preference;
import com.example.myapplication.view.BarView;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {

    private AnimationFragment mAnimationFragment;
    private WordFragment mWordFragment;


    private String[] PERMISSIONS = new String[] {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    @RequiresApi(api = Build.VERSION_CODES.R)
    private boolean hasPermission() {
        for (String str:PERMISSIONS) {
            int result = ContextCompat.checkSelfPermission(this, str);
            if(result != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
//
//        if (Environment.isExternalStorageManager()) {
//            return true;
//        }
//        Intent getPermission = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
//        startActivityForResult(getPermission, 0);
       return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.R)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(hasPermission() == false) {
            //TODO  申请权限
            ActivityCompat.requestPermissions(this, PERMISSIONS, 0);
        }
        setContentView(R.layout.activity_list_layout);

        Intent intent = getIntent();
        String userName = intent.getStringExtra(Const.USER_NAME_KEY);

        TextView userNameTv = findViewById(R.id.user_name);
        userNameTv.setText(userName);

        Intent resultIntent = new Intent();
        resultIntent.putExtra(Const.NUMBER_KEY,  91011);
        setResult(6666,  resultIntent  );

        mAnimationFragment = new AnimationFragment();
        mWordFragment = new WordFragment();

        List<Fragment> list = new ArrayList<>();

        list.add(mAnimationFragment);
        list.add(mWordFragment);

        FragmentAdapter fragmentAdapter = new FragmentAdapter(
                getSupportFragmentManager(),
                list);

        ViewPager viewPager = findViewById(R.id.content_fragment);
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setCurrentItem(0);

        BarView barView = findViewById(R.id.bar);
        barView.setViewPager(viewPager);

        Intent i = new Intent(ListViewActivity.this, MusicService.class);

        bindService(i, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
//                mWordFragment.setMusicController((MusicController) service);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, BIND_AUTO_CREATE);
    }


}
